/****************************************************************************
 * Copyright (C) 2012 Dj_Skual
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
#include "UpdateGameTDBTask.h"
#include "../../Controls/Application.h"
#include "../../Prompts/ProgressWindow.h"
#include "../../Prompts/PromptWindows.h"
#include "../../FileOperations/fileops.h"
#include "../FileDownloader.h"

//static const char GameTDB_Url[MAXPATHLEN] = "http://www.gametdb.com/wiitdb.zip?LANG=TITLESONLY&WIIWARE=TRUE";
static const char GameTDB_Url[MAXPATHLEN] = "http://www.gametdb.com/wiitdb.zip?LANG=TITLESONLY&GAMECUBE=TRUE&WIIWARE=TRUE";

UpdateGameTDBTask::UpdateGameTDBTask()
{
	result = false;
	running = true;
	zipfile = NULL;
	CopySize = 0;
	
	ThreadedTaskHandler::Instance()->AddTask(this);
}

UpdateGameTDBTask::~UpdateGameTDBTask()
{
	if(zipfile)
		delete zipfile;
}

u64 UpdateGameTDBTask::GetUnpackSize(void)
{
	u64 extractSize = 0;

	for(u32 i = 0; i < zipfile->GetItemCount(); ++i)
	{
		ArchiveFileStruct *item = zipfile->GetFileStruct(i);
		if(!item)
			continue;
		
		if(!item->isdir)
			extractSize += item->length;
	}

	return extractSize;
}

void UpdateGameTDBTask::Execute(void)
{
	if(!CreateSubfolder(Settings.GameTDBPath))
	{
		ShowError(tr("Could not create path: %s"), Settings.GameTDBPath);
		running = false;
		return;
	}
	
	if(!NetworkInitPrompt())
	{
		ShowError(tr("Network is not initiated."));
		running = false;
		return;
	}
	
	if(ProgressWindow::Instance()->IsRunning())
		ProgressWindow::Instance()->SetTitle(tr("Updating GameTDB..."));
	else
		StartProgress(tr("Updating GameTDB..."));

	char zippath[MAXPATHLEN];
	snprintf(zippath, sizeof(zippath), "%s/wiitdb.zip", Settings.GameTDBPath);
	
	int ret = DownloadFileToPath(GameTDB_Url, zippath, false);
	if(ret < 1024)
	{
		StopProgress();
		ShowError(tr("Error when downloading file: %i"), ret);
		running = false;
		return;
	}
	sleep(1);
	
	ProgressWindow::Instance()->SetTitle(tr("Calculating extract size..."));
	
	zipfile = new ZipFile(zippath);
	CopySize = GetUnpackSize();

	ProgressWindow::Instance()->SetTitle(tr("Extracting File(s):"));
	ProgressWindow::Instance()->SetCompleteValues(0, CopySize);

	std::string destPathSlash = Settings.GameTDBPath;
	if(destPathSlash[destPathSlash.size()-1] != '/')
		destPathSlash += "/";

	int extract = zipfile->ExtractAll(destPathSlash.c_str());
	
	RemoveFile(zippath);
	StopProgress();
	
	if((extract < 0) && !Application::isClosing() && extract != PROGRESS_CANCELED)
	{
		ThrowMsg(tr("Error:"), tr("Failed extracting some item(s)."));
		running = false;
		return;
	}
	
	result = true;
	running = false;
}
